//自定义jQuery插件 1.0



//向右滑动显示隐藏
jQuery.fn.slideLeftHide = function( speed, callback ) {
	this.animate({
		width : "hide",
		paddingLeft : "hide",
		paddingRight : "hide",
		marginLeft : "hide",
		marginRight : "hide"
	}, speed, callback );
};
jQuery.fn.slideLeftShow = function( speed, callback ) {
	this.animate({
		width : "show",
		paddingLeft : "show",
		paddingRight : "show",
		marginLeft : "show",
		marginRight : "show"
	}, speed, callback );
};

//算特定容器活动高度  totalH 总高度 类型 int obj ; otherH 待减去的高度 类型 int obj ; number 修正偏差 类型 int
jQuery.fn.flexHeight = function(obj) {
	//获取总高度
	if(!obj.totalH){
		totalH=$(window).height();
	}else if(typeof(obj.totalH)=="number"){
		totalH=obj.totalH;
	}else if(typeof(obj.totalH)=="object"){
		totalH=obj.totalH.height();
	};

	//获取需要减去对象的高度
	if(!obj.otherH){
		otherH=0;
	}else if(typeof(obj.otherH)=="number"){
		otherH=obj.otherH;
	}else if(typeof(obj.otherH)=="object"){
		var otherH=0;
		$.each(obj.otherH, function(i, n){
		   otherH=otherH+$(n).outerHeight(true) ;

		});
	};
	//获取偏差修正值
	if(!obj.number){obj.number=0};
    //计算高度
	$(this).each(function(index, element) {
		$(element).height(totalH-otherH-obj.number);

	});


}


//显示加载菊花
jQuery.fn.showLoading = function() {
		if(this.css("position")!="relative"){
			this.css("position","relative");
		};

		w=$(window).width();
		h=$(window).height();

		if(!$(".loading-Bg")[0]){
		    var bg='<div class="loading-bg" style="width:'+w+'px;height:'+h+'px"></div>';

		}else{
			var bg="";
		}


    	if(this.find(".loading").length==0){
			this.prepend('<div class="loading"></div>'+bg);
		};

		$(window).resize(function(e) {
			w=$(window).width();
	    	h=$(window).height();
            $(".loading-Bg").css({"width":w+"px","height":h+"px"});
        });
		//屏幕太长时调整位置
		if(this.height() > $(window).height()){
			topPx=$(document).scrollTop();
			//console.log(topPx)
			this.find(".loading").css("top",topPx);
		}else{
			topPx=this.height()/2;
			this.find(".loading").css("top",topPx);
		}
		this.find(".loading").fadeIn(200);

};
//隐藏加载菊花
jQuery.fn.hideLoading = function() {

		this.find(".loading").fadeOut(200);
		this.find(".loading-bg").remove();
		   setTimeout(function () {
		   $(this).remove(".loading");
		}, 200);
};



//替代textarea    <input contenteditable="true" type="hidden" valid='{"required":true}' msg='{"required":"请填写擅长疾病"}'  placeholder="请填写擅长疾病" value="111">
// $("input[type='hidden'][contenteditable]").contenteditable();
jQuery.fn.contenteditable = function() {

	$(this).each(function(index, element) {
        var prev = $(this).prev();
		$(this).prop('_value','');
        if(!parent){
        	parent = $(this).parents('[avalonctrl]')[0];
        }
        if(prev.is('div') && typeof(prev.attr("contenteditable"))!="undefined") {
        	//元素已存在则删除
            //console.log(prev);

            prev.remove();
        }
		  if($(element).attr("placeholder")){
			  var ph = "<span class='placeholder'>"+$(element).attr("placeholder")+"</span>";
		  }else{
			  var ph = "";
		  }

		  var div=$("<div >"+ph+"</div>");

		  div.attr("contenteditable","true");
		  div.attr("class",$(element).attr("class"));
		  div.attr("style",$(element).attr("style"));

		  //------- avalon支持---------//
		  var avalonProperties = $(element).attr('avalonProperties');
		  if(avalonProperties){
		  		var aps = avalonProperties.split(' ');
				for(var i=0;i<aps.length;i++){
				  	var kv = aps[i].split('=');
				  	if(kv.length == 2){
				  		// $(element).attr(kv[0],kv[1]);
				  		element.setAttribute(kv[0],kv[1])
				  		if(kv[0] == 'ms-attr-value'){
				  			div.attr('ms-html',kv[1]);
				  		} else if(kv[0] == 'ms-controller'){
				  			if(!controller){
				  				var controller = kv[1];
				  			}
				  			div.attr(kv[0],kv[1]);
				  		} else {
				  			div.attr(kv[0],kv[1]);
				  		}
				  	} else {
				  		// $(element).attr(kv[0],kv[0]);
				  		element.setAttribute(kv[0],kv[0])
				  		div.attr(kv[0],kv[0]);
				  	}
				}
				$(element).removeAttr('avalonProperties');
		  }
		  //------- avalon支持---------//

		  if($(element).val() ){

			  //读取input里已有的内容
			   div.html($(element).val());
		  }else{

		  }
		  //console.log($(div).html());
		  $(element).before(div);


		  //------- avalon支持---------//
		  if(avalonProperties){
			  avalon.scan($(element).prev()[0],controller);
			  avalon.scan($(element)[0],controller);
		  }
		  //------- avalon支持---------//

		  div.on("focus",function(){

			  if(!$(this).next("input[type='hidden']").val()){

				  div.html("");
			  }

		  });
		  div.on("focusout",function(){
				$(this).next("input[type='hidden']").val(this.innerHTML);
			  if(!$(this).next("input[type='hidden']").val()){

				  div.html(ph);
			  }

		  });


		  div.on("keyup change",function(){
				var cnt=$(this).html();
				//$(this).next("input[type='hidden']").val(cnt);		//兼容ie 7 8 的写法
			  $(this).next("input[type='hidden']").prop('_value',cnt);//兼容object.defineproperty
				//console.log($(this).next("input[type='hidden']").prop('_value'))
			  //console.log($(this).parent().find("input[type='hidden']").val());
		  });

		   //过滤剪贴板html标签
		   function isIe8(){
				var isIE = navigator.userAgent.toLocaleLowerCase().indexOf('msie') !== -1;
				var isLteIE8 = isIE && !+[1,];
				if( isLteIE8 && document.documentMode){
					return true;
				}else{
					return false;
				}
		   }
		   function w3cFilter(e){
				var event = window.event||e;
				var target = e.target;
				event.preventDefault();
				var txt = event.clipboardData.getData('text/plain');
				var reg=/<[^>]+>/;
				if(reg.test(txt))txt = txt.replace(/<[^>]+>/g,'');
				var sel=window.getSelection();
				var range = sel.getRangeAt(0);
				var span = document.createElement('span');
				span.innerHTML=txt;
				range.deleteContents();
				range.insertNode(span);
			}

		   function ieFilter(){
				var e=window.event;
				var target = e.srcElement;
				e.returnValue=false;
				if(window.clipboardData){
					var content = window.clipboardData.getData('text');
					var reg=/<[^>]+>/;
					if(reg.test(content)){
						content = content.replace(/<[^>]+>/g,'');
					};
					if(document.selection){
						var range = document.selection.createRange();
						range.text=content;
					}
				}
			}
		  div.on('paste',function(e){
			if(isIe8()){
				ieFilter();
			}else{
				w3cFilter(e);
			}
		  });
		  //过滤默认拖拽
		  $("*").on("drop",function(e){
		  	e.preventDefault();
		  })

	});


}
//清除输入框
jQuery.fn.clearContent = function() {
	$(this).each(function(index, element) {
		  $(element).val("");
		  if($(element).attr("placeholder")){
			  var ph = "<span class='placeholder'>"+$(element).attr("placeholder")+"</span>";
		  }else{
			  var ph = "";
		  }
		  $(element).prev("[contenteditable]").html(ph);

	});
}
//同步input值到div输入框
jQuery.fn.syncContent = function(options) {
	$(this).each(function(index, element) {

          if(options.value){
			  $(element).val(options.value);
			  $(element).prev("[contenteditable]").html(options.value);
		  }else{
			  if($(element).attr("placeholder")){
				  var ph = "<span class='placeholder'>"+$(element).attr("placeholder")+"</span>";
			  }else{
				  var ph = "";
			  }
			  $(element).prev("[contenteditable]").html(ph);
		  }

		  if(options.callback){
		  	  options.callback();
		  }

	});
}

jQuery.fn.Qsubmit = function(success,failed,beforeSubmit) {
    if($(this).is('form')){
        Qsubmit($(this), success, failed,beforeSubmit);
    };
}
jQuery.fn.Nsubmit = function(success,failed) {
    if($(this).is('form')){
        Nsubmit($(this));
    };
}
jQuery.fn.resetImgcode = function() {
    if($(this).is('form')){
    	if($(this).find('input[name=imgcode]').length > 0){
    		$(this).find('input[name=imgcode]').val('');
    	}
        if($(this).find('img[name=img]').length > 0){
        	$(this).find('img[name=img]').click();
        }
    };
}